﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ones.ServiceAccountManagement;

namespace Ones.SysAdmin
{
    public partial class AccountManagement : System.Web.UI.Page
    {
        ServiceAccountManagementClient amc = new ServiceAccountManagementClient("accountBasicHttpBinding");
        string empId = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            successfulMsg.Visible = false;
            failMsg.Visible = false;
            if (!string.IsNullOrEmpty(Session["empIdWithoutAccount"] as string))
            {
                empId = Session["empIdWithoutAccount"].ToString();
                Employee emp = amc.getEmployeeById(empId);
                txtEmpName.Text = emp.empFirstName + " " + emp.empLastName;
            }
            else
            {
                Response.Redirect("EmployeeWithoutAccount.aspx");
            }
            if (!IsPostBack)
            {
                ddlRole.DataSource = amc.getRoles();
                ddlRole.DataTextField = "RoleName";
                ddlRole.DataValueField = "RoleName";
                ddlRole.DataBind();
            }
        }

        protected void Create_Click(object sender, EventArgs e)
        {
            bool success = amc.createUserAccount(txtUsername.Text, txtUserPassword.Text, txtUserEmail.Text, empId, ddlRole.SelectedValue);
            if (success)
            {
                Session.Remove("empIdWithoutAccount");
                successfulMsg.Visible = true;
                failMsg.Visible = false;
                Response.Redirect("Accounts.aspx");
            }
            else
            {
                successfulMsg.Visible = false;
                failMsg.Visible = true;
            }
        }
    }
}